package com.ss.android.newmedia.e;

import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Message;
import android.view.LayoutInflater;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.ss.android.article.browser.R;
import com.ss.android.common.load.AsyncLoader;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.image.BaseImageManager;
import com.ss.android.image.loader.ImageLoader;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.feedback.FeedbackActivity;
import com.ss.android.newmedia.feedback.n;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public final class AlertManager implements WeakHandler.Callback {
    public final Context a;
    public final WeakHandler b;
    final BaseAppData c;
    public final String d;
    final BaseImageManager e;
    final TaskInfo f;
    public final ImageLoader g;
    final LayoutInflater h;
    public final AsyncLoader i;
    public long j;
    public long k;
    final List<com.ss.android.newmedia.f.a> l;
    private final AsyncLoader.LoaderProxy m;
    private WeakReference n;
    private final Runnable o;
    private final Comparator p;

    public AlertManager(Context arg10, BaseAppData arg11) {
        int v4 = 8;
        this.m = new com.ss.android.newmedia.e.b(this);
        this.j = 0;
        this.k = 0;
        this.l = new ArrayList(v4);
        this.o = new com.ss.android.newmedia.e.c(this);
        this.p = new com.ss.android.newmedia.e.d();
        Context v0 = (arg10 instanceof Application) ? arg10 : arg10.getApplicationContext();
        this.a = v0;
        this.c = arg11;
        this.b = new WeakHandler(this.a.getMainLooper(), this);
        this.d = this.c.getAppContext().getFeedbackAppKey();
        this.i = new AsyncLoader(this.m);
        this.e = new BaseImageManager(this.a);
        this.f = new TaskInfo();
        this.h = LayoutInflater.from(this.a);
        Resources v0_1 = this.a.getResources();
        this.g = new ImageLoader(arg10, this.f, 4, v4, 1, this.e, v0_1.getDimensionPixelSize(R.dimen.bs), v0_1.getDimensionPixelSize(R.dimen.br));
    }

    public final void a(long arg6) {
        long v2 = 0;
        if (!this.l.isEmpty()) {
            long v0 = ((long) this.l.get(0).a);
            if (v0 < v2) {
                v0 = v2;
            }

            if (arg6 <= v2) {
                arg6 = 5000;
            }

            if (this.c.cH < v0) {
                v0 = (v0 - this.c.cG) * 1000;
                if (v0 >= arg6) {
                    arg6 = v0;
                }
            }

            this.b.removeCallbacks(this.o);
            this.b.postDelayed(this.o, arg6);
            Logger.d("AlertManager", "schedule alert " + this.l.size() + " with delay " + arg6);
        }
    }

    private void a(boolean arg7, List arg8) {
        Object v0;
        Object v5 = null;
        if ((arg7) && arg8 != null && arg8.size() > 0) {
            ArrayList v2 = new ArrayList();
            Iterator v3 = arg8.iterator();
            int v1;
            int v0_1 = 0;
            for (v1 = 0; v3.hasNext(); v1 = v0_1) {
                v0 = v3.next();
                if (v0 == null) {
                    continue;
                }

                if (!((com.ss.android.newmedia.f.a) v0).f) {
                    continue;
                }
                ((List) v2).add(v0);
                v0_1 = v1 + 1;
                if (v0_1 > 20) {
                    break;
                }
            }

            Logger.v("AlertManager", "alert count " + ((List) v2).size());
            if (((List) v2).size() <= 0) {
                return;
            }

            Collections.sort(v2, this.p);
            this.l.clear();
            this.b.removeCallbacks(this.o);
            this.l.addAll(v2);
            Iterator v1_1 = this.l.iterator();
            while (v1_1.hasNext()) {
                v0 = v1_1.next();
                this.i.addTask(((com.ss.android.newmedia.f.a) v0).c, v0, v5, v5);
            }

            if (this.c.cF == null) {
                return;
            }

            if (this.c.cF.get() == null) {
                return;
            }

            this.a(0);
        }
    }

    public final void a(Dialog arg2) {
        this.n = arg2 != null ? new WeakReference(arg2) : null;
    }

    public final boolean a() {
        Object v0 = null;
        if (this.n != null) {
            v0 = this.n.get();
        }

        boolean v0_1 = v0 == null || !((Dialog) v0).isShowing() ? false : true;
        return v0_1;
    }

    public final void b() {
        this.b.removeCallbacks(this.o);
        this.i.pause();
        this.g.onStop();
    }

    public final void handleMsg(Message message) {
        int v0_2;
        List v2 = null;
        if (c.inst() != null && message != null) {
            if (message.what == 10004) {
                this.a(false, v2);
                return;
            }

            if (message.what == 10003) {
                try {
                    this.a(true, (List) message.obj);
                } catch (Exception v0) {
                }

                return;
            }

            if (message.what == 10 && ((message.obj instanceof n))) {
                Object v0_1 = message.obj;
                if (((n) v0_1).h == null) {
                    return;
                }

                if (((n) v0_1).h.size() <= 0) {
                    return;
                }

                int v6 = ((n) v0_1).h.size();
                int v5 = 0;
                while (true) {
                    if (v5 >= v6) {
                        v0_2 = 0;
                    } else if (((n) v0_1).h.get(v5).h == 1) {
                        v0_2 = 1;
                    } else {
                        ++v5;
                        continue;
                    }

                    break;
                }
                if (v0_2 == 0) {
                    return;
                }

                v0_1 = this.c.cF != null ? this.c.cF.get() : v2;
                if ((v0_1 instanceof FeedbackActivity)) {
                    return;
                }

                if (android.support.design.a.isComponentActive(((Context) v0_1))) {
                    int v1 = this.c.lastVersionCode > 0 ? 1 : 0;
                    if ((v1 | 0) == 0) {
//                        goto label_70;
                    }else {
                        e v1_1 = new e(this, ((Context) v0_1));
                        AlertDialog.Builder v0_3 = com.ss.android.j.b.createDialogBuilder(((Context) v0_1));
                        v0_3.setTitle(com.ss.android.article.browser.R.string.info_has_new_feedback).setPositiveButton(com.ss.android.article.browser.R.string.label_view, v1_1).setNegativeButton(com.ss.android.article.browser.R.string.label_cancel, ((DialogInterface.OnClickListener) v2));
                        this.a(v0_3.show());
                    }
                }

//                label_70:
                com.ss.android.newmedia.feedback.a.a();
                com.ss.android.newmedia.feedback.a.e();
            }
        }
    }
}

